package com.chengxiaoxiao.dao.impl;

import com.chengxiaoxiao.dao.OrderDao;
import com.chengxiaoxiao.domain.Order;
import com.chengxiaoxiao.utils.db.BeanHandler;
import com.chengxiaoxiao.utils.db.BeanListHandler;
import com.chengxiaoxiao.utils.db.DBAssist;
import com.chengxiaoxiao.utils.db.DBCPUtil;

import java.util.List;

public class OrderDaoImpl implements OrderDao {

    private DBAssist db = new DBAssist(DBCPUtil.getDataSource());
    @Override
    public void add(Order order) {
        db.update("insert into `order`(userId,bookId,subTime) values(?,?,?)",order.getUserId(),order.getBookId(),order.getSubTime());
    }

    @Override
    public void update(Order order) {
        db.update("update `order` set userId=?,bookId=?,subTime=? where id=?",order.getUserId(),order.getBookId(),order.getSubTime(),order.getId());

    }

    @Override
    public void delete(long id) {
        db.update("delete from `order` where id=?",id);
    }

    @Override
    public List get() {
        return (List) db.query("select * from `order`",new BeanListHandler(Order.class));
    }

    @Override
    public Order getbyId(long id) {
        return (Order) db.query("select * from `order` where id=?",new BeanHandler(Order.class),id);
    }

    @Override
    public List getListPage(int startIndex, int pageSize) {
        String sql = "select * from `order` limit ?,?";
        return (List) db.query(sql,new BeanListHandler(Order.class),startIndex,pageSize);
    }


}
